package cn.git.agent;

import cn.git.agent.enhance.ClassEnhancePluginDefine;
import cn.git.agent.interceptor.ConstructMethodInterceptPoint;
import cn.git.agent.interceptor.InstanceMethodInterceptPoint;
import cn.git.agent.interceptor.StaticMethodInterceptPoint;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher;

import static net.bytebuddy.matcher.ElementMatchers.*;

/**
 * @description: 定义springMVC的拦截插件
 * @program: bank-credit-sy
 * @author: lixuchun
 * @create: 2024-12-24
 */
public abstract class SpringMVCCommonInstrumentation extends ClassEnhancePluginDefine {

    /**
     * 拦截的注解开头结尾
     */
    private static final String MAPPING_PACKAGE_PREFIX = "org.springframework.web.bind.annotation";
    private static final String MAPPING_PACKAGE_SUFFIX = "Mapping";

    /**
     * 拦截器类
     */
    public static final String SPRINGMVC_INTERCEPTOR_CLASS = "cn.git.agent.interceptor.SpringMVCInterceptor";

    /**
     * 获取需要拦截的方法，实例拦截方法
     *
     * @return
     */
    @Override
    protected InstanceMethodInterceptPoint[] getInstanceMethodsInterceptPoints() {
        return new InstanceMethodInterceptPoint[] {
                new InstanceMethodInterceptPoint() {
                    @Override
                    public ElementMatcher<MethodDescription> getMethodsMatcher() {
                        return not(isStatic())
                                .and(
                                        isAnnotatedWith(nameStartsWith(MAPPING_PACKAGE_PREFIX)
                                                .and(nameEndsWith(MAPPING_PACKAGE_SUFFIX)))
                                );
                    }

                    @Override
                    public String getMethodInterceptor() {
                        return SPRINGMVC_INTERCEPTOR_CLASS;
                    }
                }
        };
    }

    /**
     * 获取构造方法的拦截点
     *
     * @return
     */
    @Override
    protected ConstructMethodInterceptPoint[] getConstructMethodInterceptPoint() {
        return new ConstructMethodInterceptPoint[0];
    }

    /**
     * 获取静态方法拦截点
     *
     * @return
     */
    @Override
    protected StaticMethodInterceptPoint[] getStaticMethodInterceptPoint() {
        return new StaticMethodInterceptPoint[0];
    }
}
